//index.js
//获取应用实例
const ip = '127.0.0.1';
const app = getApp()
Page({
  data: {
    // empty, loading, ok
    status: 'empty',
    selectedImgUrl: '',
    resultImgUrl: ''
  },

  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '衣物检索'
    })
  },

  // 事件处理函数
  postData: function() {
    wx.chooseImage({
      count: 1,
      sizeType: 'compressed',
      success: data => {
        this.setData({
          status: 'loading',
          selectedImgUrl: data.tempFilePaths[0]
        })
        wx.uploadFile({
          url: 'http://'+ip+':3000/search',
          filePath: data.tempFilePaths[0],
          name: 'image_file',
          success: res => {
            res = JSON.parse(res.data);
            console.log(res);
            if(res.result == 'OK'){
              this.setData({
                status: 'ok',
                resultImgUrl: res.imgUrl
              })
            }
          }
        })
      }
    })
  },

  // 测试
  test: function(){
    wx.request({
      url: 'http://localhost:3000',
      success: function(res){
        console.log(res)
      }
    })
  }

})
